﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FloodFree.Common;
using FloodFree.Web.Areas.Admin.ViewModels;
using System.Data;
using System.Data.SqlClient;
using System.Data.Entity.Infrastructure;

namespace FloodFree.Web.Areas.Admin.Controllers
{
    public class StatesController : BaseController
    {
        
        public ActionResult Index()
        {
            if (TempData["ViewData"] != null)
            {
                ViewData = (ViewDataDictionary)TempData["ViewData"];
            }
            List<State> states = db.States.ToList();
            return View(states);
        }

        [HttpGet]
        public ActionResult Create()
        {
            StateAddViewModel model = new StateAddViewModel();
            model.State = new State();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(StateAddViewModel model)
        {
            if (ModelState.IsValid)
            {
                State state = model.State;
                db.States.Add(state);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(model);
        }

        [HttpGet]
        public ActionResult Edit(int id)
        {

            State state = (from s in db.States
                           where s.Id.Equals(id)
                           select s).First();

            if (state == null)
            {
                return HttpNotFound();
            }

          

            return View(state);
        }

        [HttpPost]
        public ActionResult Edit(State state)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(state).State = EntityState.Modified;
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }
            }
            catch (DataException ex)
            {
                ModelState.AddModelError("", "Erro ao salvar os dados");
            }

            return View(state);
        }

        [HttpGet]
        public ActionResult Delete(int id)
        {
            State state = db.States
                .Where(x => x.Id == id)
                .First();

            if (state != null)
            {
                try
                {
                    db.States.Remove(state);
                    db.SaveChanges();

                }
                catch (DbUpdateException e)
                {
                    SqlException s = e.InnerException.InnerException as SqlException;
                    if (s != null && s.Number == 547)
                    {
                        ModelState.AddModelError(string.Empty,
                            string.Format("Não é possível remover pois existem cidades cadastradas para esse estado."));
                    }
                    else
                    {
                        ModelState.AddModelError("", "Erro ao remover o registro");
                    }
                }

            }
            TempData["ViewData"] = ViewData;

            return RedirectToAction("Index");
        }



    }
}
